package week05;

import org.springframework.beans.factory.support.BeanDefinitionReader;
import org.springframework.beans.factory.support.PropertiesBeanDefinitionReader;
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
import org.springframework.core.io.ClassPathResource;

/**
 * @author edward
 * @since 2021-07-20
 */
public class SpringInitializer {
    public static void main(String[] args) {
        LexicalResource resource =
//        xmlBean();
//        xmlStaticFactory();
//        annotationScan();
//        annotationRegister();
//        staticRegister();
        propertiesDefinition();
        System.out.println(resource.kindOf("language"));
    }

    private static LexicalResource xmlBean() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        return context.getBean("resInXml", LexicalResource.class);
    }

    private static LexicalResource xmlStaticFactory() {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext2.xml");
        return context.getBean("resInXml2", LexicalResource.class);
    }

    private static LexicalResource annotationScan() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext("week05");
        return context.getBean("resInAnnotation", LexicalResource.class);
    }

    private static LexicalResource annotationRegister() {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
        context.register(AppConfig.class);
        context.refresh(); // mandatory
        return context.getBean("resInAppConfig", LexicalResource.class);
    }

    private static LexicalResource staticRegister() {
        StaticApplicationContext context = new StaticApplicationContext();
        context.registerSingleton("resInStatic", LexicalResource.class);
        context.refresh();
        return context.getBean("resInStatic", LexicalResource.class);
    }

    private static LexicalResource propertiesDefinition() {
        GenericApplicationContext context = new GenericApplicationContext();
        BeanDefinitionReader propReader = new PropertiesBeanDefinitionReader(context);
        propReader.loadBeanDefinitions(new ClassPathResource("beans.properties"));
        context.refresh();
        return context.getBean("resInProperties", LexicalResource.class);
    }
}
